package com.operator.Activitys.view.pay.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.operator.Activitys.R;
import com.operator.Activitys.view.pay.adapter.KeyBoardAdapter;
import com.operator.Activitys.view.pay.listener.OnPasswordInputFinish;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 弹框里面的View
 */
public class PopupPasswordView extends RelativeLayout {

    //TODO 视图
    //支付键盘 盘版view
    private GridView gridView;
    //支付键盘
    private VirtualKeyboardView virtualKeyboardView;
    //关闭按钮
    private ImageView imgCancel;
    //支付标题
    private TextView tvPayTitle;
    //支付金额
    private TextView tvPayMoney;
    //支付提示内容
    private TextView tvPayMsg;

    //TODO 变量
    //全局上下文
    Context mContext;
    //用数组保存6个TextView，为什么用数组？
    private TextView[] tvList;
    //用数组保存6个TextView，为什么用数组？
    private ImageView[] imgList;


    private ArrayList<Map<String, String>> valueList;

    private int currentIndex = -1;    //用于记录当前输入密码格位置

    public PopupPasswordView(Context context) {
        this(context, null);
    }

    public PopupPasswordView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        //获取 布局
        View view = View.inflate(context, R.layout.layout_popup_pwd, null);

        //获取 支付键盘
        virtualKeyboardView = (VirtualKeyboardView) view.findViewById(R.id.virtualKeyboardView);
        gridView = virtualKeyboardView.getGridView();

        //获取关闭按钮
        imgCancel = (ImageView) view.findViewById(R.id.img_cancel);
        //获取支付标题
        tvPayTitle = (TextView) view.findViewById(R.id.tv_pay_title);
        //获取支付金额
        tvPayMoney = (TextView) view.findViewById(R.id.tv_pay_money);
        //获取支付提示内容
        tvPayMsg = (TextView) view.findViewById(R.id.tv_pay_msg);


        //TODO 初始化
        //获取键盘上的 数字
        initValueList();
        //初始化 视图集合
        initView(view);
        //键盘点击事件监听
        setupView();
        //添加view 到自定义控件
        addView(view);
    }

    /**
     * 初始化 视图集合
     * @param view
     */
    private void initView(View view) {

        tvList = new TextView[6];

        imgList = new ImageView[6];

        tvList[0] = (TextView) view.findViewById(R.id.tv_pass1);
        tvList[1] = (TextView) view.findViewById(R.id.tv_pass2);
        tvList[2] = (TextView) view.findViewById(R.id.tv_pass3);
        tvList[3] = (TextView) view.findViewById(R.id.tv_pass4);
        tvList[4] = (TextView) view.findViewById(R.id.tv_pass5);
        tvList[5] = (TextView) view.findViewById(R.id.tv_pass6);


        imgList[0] = (ImageView) view.findViewById(R.id.img_pass1);
        imgList[1] = (ImageView) view.findViewById(R.id.img_pass2);
        imgList[2] = (ImageView) view.findViewById(R.id.img_pass3);
        imgList[3] = (ImageView) view.findViewById(R.id.img_pass4);
        imgList[4] = (ImageView) view.findViewById(R.id.img_pass5);
        imgList[5] = (ImageView) view.findViewById(R.id.img_pass6);

    }

    /**
     * 获取键盘上的 数字
     * ps：这里，我们没有使用默认的数字键盘，因为第10个数字不显示.而是空白
     */

    private void initValueList() {

        valueList = new ArrayList<>();

        // 初始化按钮上应该显示的数字
        for (int i = 1; i < 13; i++) {
            Map<String, String> map = new HashMap<String, String>();
            if (i < 10) {
                map.put("name", String.valueOf(i));
            } else if (i == 10) {
                map.put("name", "");
            } else if (i == 11) {
                map.put("name", String.valueOf(0));
            } else if (i == 12) {
                map.put("name", "");
            }
            valueList.add(map);
        }
    }

    /**
     * 键盘点击事件监听
     */
    private void setupView() {

        // 这里、重新为数字键盘gridView设置了Adapter
        KeyBoardAdapter keyBoardAdapter = new KeyBoardAdapter(mContext, valueList);
        gridView.setAdapter(keyBoardAdapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position < 11 && position != 9) {    //点击0~9按钮

                    if (currentIndex >= -1 && currentIndex < 5) {      //判断输入位置————要小心数组越界
                        ++currentIndex;
                        tvList[currentIndex].setText(valueList.get(position).get("name"));

                        tvList[currentIndex].setVisibility(View.INVISIBLE);
                        imgList[currentIndex].setVisibility(View.VISIBLE);
                    }
                } else {
                    if (position == 11) {      //点击退格键
                        if (currentIndex - 1 >= -1) {      //判断是否删除完毕————要小心数组越界

                            tvList[currentIndex].setText("");

                            tvList[currentIndex].setVisibility(View.VISIBLE);
                            imgList[currentIndex].setVisibility(View.INVISIBLE);

                            currentIndex--;
                        }
                    }
                }
            }
        });
    }

    /**
     * 设置监听方法，在第6位输入完成后触发
     * @param pass
     */
    public void setOnFinishInput(final OnPasswordInputFinish pass) {
        tvList[5].addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (s.toString().length() == 1) {

                    String strPassword = "";     //每次触发都要先将strPassword置空，再重新获取，避免由于输入删除再输入造成混乱

                    for (int i = 0; i < 6; i++) {
                        strPassword += tvList[i].getText().toString().trim();
                    }
                    pass.inputFinish(strPassword);    //接口中要实现的方法，完成密码输入完成后的响应逻辑
                }
            }
        });
    }

    /**
     * 提供给外部 调用 设置 内容信息方法
     * @param title     标题
     * @param money     金额
     */
    public void setPayMsg(String title,double money){
        //设置支付标题
        tvPayTitle.setText(title);
        //设置金额
        tvPayMoney.setText("￥"+money);
    }

    /**
     * 提供外部 给外部 获取 软件盘 view 调用方法
     * @return
     */
    public VirtualKeyboardView getVirtualKeyboardView() {

        return virtualKeyboardView;
    }

    /**
     * 提供给外部 取消按钮 view 调用方法
     * @return
     */
    public ImageView getImgCancel() {
        return imgCancel;
    }
}
